package webData

import (
	"errors"
	"fmt"
	"strconv"

	"go-data-washing/utils"

	"github.com/jinzhu/gorm"
)

type WebData struct {
	Id                     string `json:"id" gorm:"column:id"`
	Company_name           string `json:"company_name" gorm:"column:company_name"`
	Platform_domain        string `json:"platform_domain" gorm:"column:platform_domain"`
	Web_registration_name  string `json:"web_registration_name" gorm:"column:web_registration_name"`
	Subject_type           string `json:"subject_type" gorm:"column:subject_type"`
	Store_url              string `json:"store_url" gorm:"column:store_url"`
	Transaction_type       string `json:"transaction_type" gorm:"column:transaction_type"`
	Site_title             string `json:"site_title" gorm:"column:site_title"`
	Site_desc              string `json:"site_desc" gorm:"column:site_desc"`
	Store_key              string `json:"store_key" gorm:"column:store_key"`
	Subject_name           string `json:"subject_name" gorm:"column:subject_name"`
	Uniform_credit_code    string `json:"uniform_credit_code" gorm:"column:uniform_credit_code"`
	Native_city            string `json:"native_city" gorm:"column:native_city"`
	Area                   string `json:"area" gorm:"column:area"`
	Registered_capital     string `json:"registered_capital" gorm:"column:registered_capital"`
	Enterprise_state       string `json:"enterprise_state" gorm:"column:enterprise_state"`
	Legal_person           string `json:"legal_person" gorm:"column:legal_person"`
	Registration_time      string `json:"registration_time" gorm:"column:registration_time"`
	Enterprise_type        string `json:"enterprise_type" gorm:"column:enterprise_type"`
	Registration_authority string `json:"registration_authority" gorm:"column:registration_authority"`
	Start_datetime         string `json:"start_datetime" gorm:"column:start_datetime"`
	End_datetime           string `json:"end_datetime" gorm:"column:end_datetime"`
	Company_address        string `json:"company_address" gorm:"column:company_address"`
	Scope_business         string `json:"scope_business" gorm:"column:scope_business"`
	Dirty_types            string `json:"dirty_types" gorm:"column:dirty_types"`
	Clean                  string `json:"clean" gorm:"column:clean"`
	Clean_count            string `json:"clean_count" gorm:"column:clean_count"`
}

func (w WebData) TableName() string {
	return "web"
}

type WebClean struct {
	Id                     string `json:"id" gorm:"column:id"`
	Company_name           string `json:"company_name" gorm:"column:company_name"`
	Platform_domain        string `json:"platform_domain" gorm:"column:platform_domain"`
	Web_registration_name  string `json:"web_registration_name" gorm:"column:web_registration_name"`
	Subject_type           string `json:"subject_type" gorm:"column:subject_type"`
	Store_url              string `json:"store_url" gorm:"column:store_url"`
	Transaction_type       string `json:"transaction_type" gorm:"column:transaction_type"`
	Site_title             string `json:"site_title" gorm:"column:site_title"`
	Site_desc              string `json:"site_desc" gorm:"column:site_desc"`
	Store_key              string `json:"store_key" gorm:"column:store_key"`
	Subject_name           string `json:"subject_name" gorm:"column:subject_name"`
	Uniform_credit_code    string `json:"uniform_credit_code" gorm:"column:uniform_credit_code"`
	Native_city            string `json:"native_city" gorm:"column:native_city"`
	Area                   string `json:"area" gorm:"column:area"`
	Registered_capital     string `json:"registered_capital" gorm:"column:registered_capital"`
	Enterprise_state       string `json:"enterprise_state" gorm:"column:enterprise_state"`
	Legal_person           string `json:"legal_person" gorm:"column:legal_person"`
	Registration_time      string `json:"registration_time" gorm:"column:registration_time"`
	Enterprise_type        string `json:"enterprise_type" gorm:"column:enterprise_type"`
	Registration_authority string `json:"registration_authority" gorm:"column:registration_authority"`
	Start_datetime         string `json:"start_datetime" gorm:"column:start_datetime"`
	End_datetime           string `json:"end_datetime" gorm:"column:end_datetime"`
	Company_address        string `json:"company_address" gorm:"column:company_address"`
	Scope_business         string `json:"scope_business" gorm:"column:scope_business"`
	Dirty_types            string `json:"dirty_types" gorm:"column:dirty_types"`
	Clean                  string `json:"clean" gorm:"column:clean"`
	Clean_count            string `json:"clean_count" gorm:"column:clean_count"`
}

func (w WebClean) TableName() string {
	return "web_clean"
}

type WebDirty struct {
	Id                     string `json:"id" gorm:"column:id"`
	Company_name           string `json:"company_name" gorm:"column:company_name"`
	Platform_domain        string `json:"platform_domain" gorm:"column:platform_domain"`
	Web_registration_name  string `json:"web_registration_name" gorm:"column:web_registration_name"`
	Subject_type           string `json:"subject_type" gorm:"column:subject_type"`
	Store_url              string `json:"store_url" gorm:"column:store_url"`
	Transaction_type       string `json:"transaction_type" gorm:"column:transaction_type"`
	Site_title             string `json:"site_title" gorm:"column:site_title"`
	Site_desc              string `json:"site_desc" gorm:"column:site_desc"`
	Store_key              string `json:"store_key" gorm:"column:store_key"`
	Subject_name           string `json:"subject_name" gorm:"column:subject_name"`
	Uniform_credit_code    string `json:"uniform_credit_code" gorm:"column:uniform_credit_code"`
	Native_city            string `json:"native_city" gorm:"column:native_city"`
	Area                   string `json:"area" gorm:"column:area"`
	Registered_capital     string `json:"registered_capital" gorm:"column:registered_capital"`
	Enterprise_state       string `json:"enterprise_state" gorm:"column:enterprise_state"`
	Legal_person           string `json:"legal_person" gorm:"column:legal_person"`
	Registration_time      string `json:"registration_time" gorm:"column:registration_time"`
	Enterprise_type        string `json:"enterprise_type" gorm:"column:enterprise_type"`
	Registration_authority string `json:"registration_authority" gorm:"column:registration_authority"`
	Start_datetime         string `json:"start_datetime" gorm:"column:start_datetime"`
	End_datetime           string `json:"end_datetime" gorm:"column:end_datetime"`
	Company_address        string `json:"company_address" gorm:"column:company_address"`
	Scope_business         string `json:"scope_business" gorm:"column:scope_business"`
	Dirty_types            string `json:"dirty_types" gorm:"column:dirty_types"`
	Clean                  string `json:"clean" gorm:"column:clean"`
}

func (w WebDirty) TableName() string {
	return "web_dirty"
}

type WebRead struct {
	CleanCount int
	PageSize   int
	Current    int
	End        int
	Total      int
}

func (w *WebRead) Read(db *gorm.DB) ([]map[string]string, error) {

	current := w.Current + 1
	cache := []WebData{}
	rows := []map[string]string{}

	// 查询边界
	if current > w.End {
		return nil, errors.New("EOF")
	}

	db.Limit(w.PageSize).Offset(0).Where("clean = ? AND clean_count = ? ", "0", strconv.Itoa(w.CleanCount)).Find(&cache)
	fmt.Println("PAGE: ", current)
	for _, item := range cache {
		rows = append(rows, utils.ObjToMap(item))
	}

	// 查询数据为空
	if len(rows) <= 0 {
		return nil, errors.New("EOF")
	}

	w.Current = current
	return rows, nil

}

func (w *WebRead) Update(db *gorm.DB, rows []map[string]string) error {

	for _, item := range rows {
		current := WebData{}
		utils.MapToObj(&item, &current)
		db.Save(&current)
	}

	return nil
}

type WebCleanWrite struct {
	count int
}

func (w *WebCleanWrite) Write(db *gorm.DB, rows []map[string]string) error {

	for _, item := range rows {
		current := WebClean{}
		utils.MapToObj(&item, &current)
		db.Create(current)
	}

	return nil
}

type WebDirtyWrite struct {
	count int
}

func (w *WebDirtyWrite) Write(db *gorm.DB, rows []map[string]string) error {

	for _, item := range rows {
		current := WebDirty{}
		utils.MapToObj(&item, &current)
		db.Create(current)
	}

	return nil
}
